
import urllib.request
import os

import json


response = urllib.request.urlopen("http://www.baidu.com")
data = response.read().decode("utf-8")
#print(data)


path = r"E:\python\spider-file"
with open(os.path.join(path, "百度.html"), 'w', encoding="utf-8", errors="ignore") as f:
   f.write(data)

# 高级写法, 直接将爬取到的网页存到文件 , 把网络图片存储到本地图片文件内
urllib.request.urlretrieve("http://www.baidu.com", os.path.join(path, "百度2.html"))


# post 打包参数到请求体
url = "http://www.xx.com/login"
pastDataStr = '{"name":"meixl","password":"123456"}'
postData = json.dump(pastDataStr)

byteData = urllib.parse.urlencode(postData).encode("utf-8")
req = urllib.request.Request(url, byteData)
req.add_header("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36")
response = urllib.request.urlopen(req)
# post 打包参数到请求体